package com.winchain.chainbill.tbill.biz.rx;

import com.shell.bee.base.utils.MoneyUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.contract.ContService;
import com.shell.bee.contract.ContTemplateHelper;
import com.shell.bee.contract.po.ContBas;
import com.shell.bee.contract.po.ContProt;
import com.shell.bee.contract.po.ContTmpl;
import com.shell.feign.PubProtocolFeign;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.shell.framework.utils.SysDateUtil;
import com.shell.governor.modules.po.PubProtocol;
import com.winchain.chainbill.basic.cus.po.CusRec;
import com.winchain.chainbill.basic.cus.service.CusRecService;
import com.winchain.chainbill.esign.service.ContBasSnService;
import com.winchain.chainbill.tbill.base.po.TbDct;
import com.winchain.chainbill.tbill.base.po.TbRec;
import com.winchain.chainbill.tbill.biz.TBill;
import com.winchain.chainbill.tbill.biz.TBillEvent;
import com.winchain.chainbill.tbill.biz.contract.keyword.EsignTbKeyword;
import com.winchain.chainbill.util.TranslateUtil;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class TBillDctContractSponsorSubcriber extends OnErrorNotImplementedSubscriber<TBillEvent<? extends Object>> {
	@Autowired
	private PubProtocolFeign pubProtocolFeign;
	@Autowired
	private ContService contService;
	@Autowired
	private CusRecService cusRecService;
	@Autowired
	private ContBasSnService contBasSnService;

	public void onApplicationEvent(TBillEvent<?> t) {
		createContForLocal(t);
	}

	private ContBas createContForLocal(TBillEvent<?> t) {
		TbRec tbRec = t.getBill().getRec();
		TbDct tbDct = (TbDct) t.getBill().getSource();
		String location = t.getStringValue("location");

		String bizId = tbDct.getBillId();
		String bizType = "50";
		String protTitle = "����";
		String sponsorId = tbDct.getSellerId();
		String acceptorId = tbDct.getFinId();
		String witness = tbDct.getFinId();
		String protocolType = "tbdct_proto";
		String grpId = tbRec.getGrpId();
		if (tbDct == null) {
			return null;
		}
		PubProtocol protocol = this.pubProtocolFeign.getPubProtocolByTypeAndGrpId(protocolType, grpId, null);
		List<ContTmpl> tmpls = this.contService.getContTmpl(bizType);
		List<ContProt> prots = null;
		CusRec cus = (CusRec) this.cusRecService.selectByKey(sponsorId);
		String contSn = this.contBasSnService.createContSn(cus.getGrpId());
		if ((protocol != null) && (tmpls != null) && (tmpls.size() > 0)) {
			prots = new ArrayList();
			String oriContent = protocol.getContent();
			String content = genContent(oriContent, tbRec, tbDct, contSn, location);
			ContTmpl tmpl = (ContTmpl) tmpls.get(0);
			ContProt prot = this.contService.genProtWithContent(tmpl.getTmplId(), protTitle, content, true);
			prots.add(prot);
		}
		ContBas cont = new ContBas();
		String contId = SystemUtil.getUUID();
		cont.setContId(contId);
		cont.setContSn(contSn);
		cont.setContTitle(protTitle);
		cont.setBizType(bizType);
		cont.setBizId(bizId);
		cont.setSponsor(sponsorId);
		cont.setAcceptor(acceptorId);
		cont.setWitness(witness);
		cont.setContDate(SysDateUtil.getSystemDate());
		cont.setCreateTime(SysDateUtil.getSystemDateTime());
		cont.setContState("1");
		cont.setContProgress("0");
		this.contService.saveCont(cont, prots);
		return cont;
	}

	private String genContent(String content, TbRec rec, TbDct source, String contSn, String location) {
		EsignTbKeyword keyword = new EsignTbKeyword();
		keyword.setTbContSn(contSn);
		keyword.settBillAmt(MoneyUtil.number2CNMoney(rec.getBillAmt()));
		keyword.settBillCode(rec.getBillCode());
		keyword.setTbSndDate(SysDateUtil.parseDate(rec.getSndDate(), "yyyyMMdd", "yyyy��MM��dd��"));
		keyword.setTbExpDate(SysDateUtil.parseDate(rec.getExpDate(), "yyyyMMdd", "yyyy��MM��dd��"));
		keyword.setTbRate(genRateStr(source.getVarRate(), source.getFixedRate(), source.getEntVarRate()));
		CusRec buyer = (CusRec) this.cusRecService.selectByKey(source.getBuyerId());
		keyword.setTbFocalName(buyer.getCusName());

		CusRec seller = (CusRec) this.cusRecService.selectByKey(source.getSellerId());
		keyword.setTbJfCompanyName(seller.getCusName());
		keyword.setTbDate(SysDateUtil.parseDate(SysDateUtil.getSystemDate(), "yyyyMMdd", "yyyy��MM��dd��"));
		keyword.setTbSignLocation(location);
		keyword.setTbJfLrName(seller.getLrName());

		CusRec finner = (CusRec) this.cusRecService.selectByKey(source.getFinId());
		keyword.setTbYfCompanyName(finner.getCusName());
		keyword.setTbYfLrName(finner.getLrName());
		try {
			String billType = TranslateUtil.getConstantName("com.winchain.chainbill.constant.TBillConstants", "TbType",
					rec.getBillType());
			keyword.settBillType(billType);
		} catch (Exception e) {
			this.logger.info("����������" + e.getMessage());
		}
		return ContTemplateHelper.replaceContent(content, keyword);
	}

	private String genRateStr(BigDecimal varRate, BigDecimal fixedRate, BigDecimal entRate) {
		BigDecimal result = fixedRate;
		if (varRate != null) {
			result = result.add(varRate);
		}
		if (entRate != null) {
			result = result.add(entRate);
		}
		return result.stripTrailingZeros().toPlainString() + " %";
	}
}
